<?php

/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2020/5/26 09:57
 */

namespace Eduxplus\WebsiteBundle\Service;


use Eduxplus\CoreBundle\Lib\Base\AppBaseService;
use Eduxplus\CoreBundle\Lib\Service\Base\SmsService;
use Eduxplus\CoreBundle\Lib\Service\CacheService;
use Eduxplus\CoreBundle\Lib\Service\CaptchaService;
use Eduxplus\EduxBundle\Entity\MallMobileSmsCode;

class GlobService extends AppBaseService
{
    protected $captchaService;
    protected $cacheService;
    protected $smsService;

    const SMS_KEY = "SMS_KEY";
    const SMS_TIME_KEY = "SMS_TIME_KEY";

    public function __construct(CaptchaService $captchaService, CacheService $cacheService, SmsService $smsService)
    {
        $this->captchaService = $captchaService;
        $this->cacheService = $cacheService;
        $this->smsService = $smsService;
    }


    /**
     * 检查 图形验证码
     * @param $imgCode
     * @param $type
     * @return bool
     */
    public function checkImgCaptcha($imgCode, $type)
    {
        $session = $this->session();
        $rs =  $this->captchaService->check($session, $imgCode, $type);
        $this->captchaService->clear($session, $type);
        return $rs;
    }


    /**
     * 发送短信验证码
     *
     * @param $mobile
     * @param $type
     * @return bool
     */
    public function sendCaptcha($mobile, $type)
    {
        $value = mt_rand(100000, 999999);
        $key = self::SMS_KEY . ":" . $mobile . ":" . $type;
        $this->cacheService->set($key, $value, 70);

        if (!$this->checkTimes($mobile)) return false;
        $result  = $this->smsService->sendCaptcha($mobile, $value);
        if (!$result) return false;
        //保存
        $model = new MallMobileSmsCode();
        $model->setType($type);
        $model->setCode($value);
        $model->setMobile($mobile);
        $this->db()->save($model);
        return true;
    }

    /**
     * 检查短信每天发送次数
     *
     * @param $mobile
     * @return bool
     */
    protected function checkTimes($mobile)
    {
        $key = self::SMS_TIME_KEY . ":" . date('Y-m-d') . ":" . $mobile;
        $times = (int) $this->cacheService->get($key);
        $smsTimes = $this->getOption("app.sms.times");
        if ($times > ($smsTimes - 1))  return $this->error()->add("已超过当天准许该手机发送短信的最大次数!");
        $time = strtotime(date('Y-m-d') . " 23:59:59");
        $this->cacheService->setExpireAt($key, $times + 1, $time);
        return true;
    }


    /**
     * 检查短信验证码
     *
     * @param $code
     * @param $mobile
     * @param $type
     * @return bool
     */
    public function checkSmsCaptcha($code, $mobile, $type)
    {
        $env = $this->getEnv();
        if ($env == 'dev') return true;
        $key = self::SMS_KEY . ":" . $mobile . ":" . $type;
        $oldCode = $this->cacheService->get($key);
        if (!$oldCode) return $this->error()->add("短信验证码错误或者已过期！");
        return $oldCode == $code;
    }

    public function getOneSentenceADay()
    {
        $list = [
            "代码写得越急，程序跑得越慢。",
            "在存储日期中的年份的时候，请使用四位数字。",
            "如果你发现特殊情况太多，那你肯定是用错方法了。",
            "程序错误中，80% 是语法错误，16% 是简单的逻辑错误，0.8% 才是困难的问题。",
            "东西没坏，就别乱修。",
            "不要站着调试程序，那会使得你的耐心减半，你需要的是全神贯注。",
            "测试只能证明程序有错误，而不能证明程序没有错误。",
            "修正程序错误的第一步是要复现这个错误。",
            "不要基于想象开发，要基于原型开发。",
            "我没有写过诗，但有人说我写过的代码，像诗一样优雅。",
        ];
        return $list[mt_rand(0, count($list) - 1)];
    }
}
